<?php
namespace Admin\Controller;

class ImgController extends AdminController
{
	public function index ()
	{
		$id = I ('get.id/d');
		$data = M ('img')->where ('mid='.$id)->select ();
		//var_dump($data);exit;
		if (!$data) {
			$this->assign ('row','暂无搜索内容...');
		}
		$this->assign ('d',$id);
		$this->assign ('list',$data);
		$this->display ();
	}
	public function add ()
	{
		/*var_dump($_GET);
		exit;*/
		$img = D ('img');
		if (!$img->create ($_GET)) {
			$this->error ($img->getError ());
		} else {
			if ($img->add ()) {
				$this->success ('添加成功',U ('Img/index',array ('id'=>$_GET['mid'])));
			} else {
				$this->error ('添加失败');
			}
		}
	}
	public function update ()
	{
		//接收ID
    	$id = I('get.id/d');
    	// exit;
    	// echo $id;
    	
    	$face = M('img')->field('face')->where('id='.$id)->find();
    	// var_dump($face);exit;
    	if($face['face'] == 0){
    		$data['face'] = 1;
    		$str = '封面';
    	}else{
    		$data['face'] = 0;
    		$str = '非封';

    	}
    	
    	if (M('img')->where('id='.$id)->save($data)) {
    		// $this->success('修改成功');
    		echo $str;
    	} else {
    		// $this->error('修改失败');
    		echo M('img')->where('id='.$id)->save($data);
    	}
	}
	public function delete ()
	{
		$id = I ('get.id/d');
		$list = M('img')->find($id);
		$file ="./Uploads/".substr($list['icon'],0,8).'/'.substr($list['icon'],8);
		if(M('img')->delete ($id) && unlink ($file)) {
			$this->success ('删除成功');
		} else {
			$this->error ('删除失败');
		}
	}
	public function upload () 
	{
		$upload = new \Think\Upload ();
		$upload->maxSize = 3145728; //设置上传文件的大小
		$upload->exts = array ('jpg','gif','png','jpeg'); //设置文件的上传类型
		$upload->subName = array('date','Ymd'); //设置保存的子目录路径
		$upload->rootPath = './Uploads/'; //上传文件的根路径
		$upload->savePath = ''; //上传文件相对于根路径的保存路径
		$info = $upload->upload ();
		/*var_dump($info); exit;*/
		if (!$info) {
			$this->error($upload->getError());
		} else {
			$icon = date('Ymd').$info['icon']['savename'];
			/*var_dump($info); exit;*/
			$this->success('上传成功',U('Img/add',array('mid'=>$_POST['mid'],'icon'=>$icon)));
		}
	}
}